package com.greate.rabbit.springbootrabbitmqdemo.work;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class WorkConsumer {
	/**
	 * 消费者1
	 */
	@RabbitListener(queuesToDeclare = @Queue("work"))
	public void receive1(String message) {
		System.out.println("消费者1 :" + message);
	}

	/**
	 * 消费者2
	 */
	@RabbitListener(queuesToDeclare = @Queue("work"))
	public void receive2(Message message, Channel channel) {
		try {
			// 改为手动确认
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
			Thread.sleep(5000);
			System.out.println("消费者2 :" + new String(message.getBody()));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
